package a.happy.town.twobook;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @BelongsProject: twobook
 * @BelongsPackage: a.happy.town.twobook
 * @Author: ByHappy  (Email:15188881427@163.c0m)
 * @CreateTime: 2023-07-27 23:08
 * @Description: IO版增删改查
 * @Version: 1.0
 **/
public class BookArr {

    // 本地文件路径
    private static File file = new File("books.txt");

    /**
     * @param
     * @author ByHappy
     * @date 23:17 2023/7/27
     **/
    public static void HappyBookhouse() {
        // 创建一个集合：用于存放相同的个体--->书籍对象   -----注意作用范围，将list放在循环外面
        while (true) {
            // 打印菜单
            System.out.println("----欢迎来到高兴书屋----");
            System.out.println("1.展示书籍");
            System.out.println("2.上新书籍");
            System.out.println("3.下架书籍");
            System.out.println("4.退出应用");

            // 键盘录入 scanner
            Scanner sc = new Scanner(System.in);
            // 设置键盘录入序号标记
            int choice = 0;
            try {
                choice = sc.nextInt();
                // 判断输入范围是否小于0大于4
                if (choice <= 0 || choice > 4) {
                    // 指令不在范围返回友好提示
                    System.out.println("您的输入指令错误，请重新输入...");
                }
            } catch (Exception e) {
                // 因为输入的要求是int，也有可能是char,所以为了代码健壮性，catch一下
                System.out.println("您的输入指令不规范，请重新输入...");
                sc = new Scanner(System.in);
            }
            // 根据choice录入序号进行判断
            //  查
            if (choice == 1) {
                System.out.println("【高兴书屋】>>>>>>1.展示书籍");
                String line;
                try {
                    // 从本地文件读取
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    try {
                        // while 循环 遍历本地书籍信息
                        while ((line = br.readLine()) != null) {
                            System.out.println(line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

            //  增
            if (choice == 2) {
                System.out.println("【高兴书屋】>>>>>>2.上新书籍");
                //键盘录入书信息
                System.out.println("请录入书籍编号：");
                String bNo = sc.next();
                System.out.println("请录入书籍名字：");
                String bName = sc.next();
                System.out.println("请录入书籍作者：");
                String bAuthor = sc.next();
                //每上新一本书籍，就要创建一本新的书籍对象
                try {
                    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
                    bufferedWriter.write(bNo + " " + bName + " " + bAuthor);
                    bufferedWriter.newLine();
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    System.out.println("书籍添加成功");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //  删
            if (choice == 3) {
                System.out.println("【高兴书屋】>>>>>>3.下架书籍");
                // 录入你要下架书籍的编号
                System.out.println("请录入你要下架书籍的编号：");
                String isDel = sc.next();
                try {
                    // 从本地文件读取
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    try {
                        ArrayList<String> array = new ArrayList<>();
                        String line;
                        // while 循环 遍历本地书籍信息
                        while ((line = br.readLine()) != null) {
                            // 把遍历到的书籍添加到新建的集合里
                            array.add(line);
                            // 然后再for循环遍历找出集合里对应的正确信息，并删除
                            for (int i = 0; i < array.size(); i++) {
                                String[] str = array.get(i).split(" ");
                                if (isDel.equals(str[0])) {
                                    array.remove(i);
                                    break;
                                }
                            }
                        }
                        // 记得关闭流
                        br.close();

                        // 把集合写回流里
                        BufferedWriter buff = new BufferedWriter(new FileWriter(file));
                        for (String s : array) {
                            buff.write(s + "\n");
                        }
                        buff.close();
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e) {
                    //
                    e.printStackTrace();
                }
            }

            //  退出指令
            if (choice == 4) {
                System.out.println("【高兴书屋】>>>>>>4.退出应用");
                System.out.println("感谢使用，再见！！！");
                System.exit(0);
            }


        }


    }
}
